package com.ljc.controller;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ljc.dto.Result;
import com.ljc.entity.Expert;
import com.ljc.entity.Notification;
import com.ljc.entity.User;
import com.ljc.service.ExpertService;
import com.ljc.service.NotificationService;
import com.ljc.service.UserService;
import com.ljc.util.JWTUtil;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
@ResponseBody
@CrossOrigin
@RequestMapping("notification")
public class NotificationController {

    @Autowired
    private NotificationService notificationService;

    @Autowired
    private UserService userService;

    @Autowired
    private ExpertService expertService;

    @RequestMapping("insert")
    public Result insert(HttpServletRequest request, @RequestBody Notification notification) {
        Result result = new Result();
        String token = request.getHeader("accessToken");
        DecodedJWT decodedJWT = JWTUtil.getTokenInfo(token);
        String id = decodedJWT.getClaim("id").asString();
        notification.setAdmitId(Integer.valueOf(id));
        notification.setCreatedTime(new Date());
        try {
            notificationService.insert(notification);
            result.setStatus(1).setMsg("新增通知成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setStatus(0).setMsg("新增通知失败");
        }


        return result;
    }

    @RequestMapping("getByAdmit")
    public Result getByAdmit(HttpServletRequest request, @RequestParam("pageNum") Integer pageNum,
                             @RequestParam("pageSize") Integer PageSize, @RequestParam int type) {
        Result result = new Result();
        String token = request.getHeader("accessToken");
        DecodedJWT decodedJWT = JWTUtil.getTokenInfo(token);
        String id = decodedJWT.getClaim("id").asString();
        PageHelper.startPage(pageNum, PageSize);
        List<Notification> byAdmit = notificationService.getByAdmit(Integer.valueOf(id), type);
        PageInfo<Notification> notificationPageInfo = new PageInfo<>(byAdmit);
        result.setStatus(1).setMsg("获取通知成功").setData(notificationPageInfo);
        return result;
    }

    @RequestMapping("getOne")
    public Result getOne(int id) {
        Result result = new Result();
        Notification one = notificationService.getOne(id);
        Result 获取通知成功 = result.setStatus(1).setMsg("获取通知成功").setData(one);
        return result;
    }

    @RequestMapping("search")
    public Result search(HttpServletRequest request, @RequestBody Notification notification) {
        Result result = new Result();
        String token = request.getHeader("accessToken");
        DecodedJWT decodedJWT = JWTUtil.getTokenInfo(token);
        String id = decodedJWT.getClaim("id").asString();
        notification.setAdmitId(Integer.valueOf(id));
        PageHelper.startPage(notification.getPageNum(), notification.getPageSize());
        List<Notification> search = notificationService.search(notification);
        PageInfo<Notification> notificationPageInfo = new PageInfo<>(search);
        result.setStatus(1).setMsg("搜索通知成功").setData(notificationPageInfo);
        return result;
    }

    @RequestMapping("delete")
    public Result delete(@RequestParam int id){
        Result result = new Result();
        try {
            notificationService.delete(id);
            result.setStatus(1).setMsg("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("删除失败").setStatus(0);
        }
        return  result;
    }
    @RequestMapping("update")
    public  Result update(@RequestBody Notification notification){
        Result result = new Result();
        try {
            notificationService.update(notification);
            result.setStatus(1).setMsg("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setStatus(0).setMsg("修改失败");
        }
        return  result;
    }

    @RequestMapping("getByUser")
    public Result getByUser(HttpServletRequest request, @RequestParam("pageNum") Integer pageNum,
                             @RequestParam("pageSize") Integer PageSize, @RequestParam int type) {
        Result result = new Result();
        String token = request.getHeader("accessToken");
        DecodedJWT decodedJWT = JWTUtil.getTokenInfo(token);
        String id = decodedJWT.getClaim("id").asString();
        User user = userService.getOne(Integer.valueOf(id));
        int deanId = user.getDeanId();
        PageHelper.startPage(pageNum, PageSize);
        List<Notification> byAdmit = notificationService.getByAdmit(deanId, type);
        PageInfo<Notification> notificationPageInfo = new PageInfo<>(byAdmit);
        result.setStatus(1).setMsg("获取通知成功").setData(notificationPageInfo);
        return result;
    }
    @RequestMapping("getByExpert")
    public Result getByExpert(HttpServletRequest request, @RequestParam("pageNum") Integer pageNum,
                            @RequestParam("pageSize") Integer PageSize, @RequestParam int type) {
        Result result = new Result();
        String token = request.getHeader("accessToken");
        DecodedJWT decodedJWT = JWTUtil.getTokenInfo(token);
        String id = decodedJWT.getClaim("id").asString();
        Expert expertById = expertService.findExpertById(Integer.valueOf(id));
        int deanId = expertById.getDeanId();
        PageHelper.startPage(pageNum, PageSize);
        List<Notification> byAdmit = notificationService.getByAdmit(deanId, type);
        PageInfo<Notification> notificationPageInfo = new PageInfo<>(byAdmit);
        result.setStatus(1).setMsg("获取通知成功").setData(notificationPageInfo);
        return result;
    }
}
